/**
 * 
 */
package com.fita.admin.visitor.impl;

import com.demo2do.core.persistence.GenericDaoSupport;
import com.fita.admin.service.DataService;
import com.fita.admin.service.authority.PrincipalService;
import com.fita.admin.service.data.*;
import com.fita.entity.authority.Role;
import com.fita.entity.coupon.Coupon;
import com.fita.entity.coupon.CouponTemplate;
import com.fita.entity.coupon.stat.CouponTemplateStat;
import com.fita.entity.data.*;
import com.fita.entity.order.Order;
import com.fita.entity.order.PaymentRequest;
import com.fita.entity.principal.District;
import com.fita.entity.principal.Principal;
import com.fita.entity.set.Set;
import com.fita.entity.set.SetCategory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author LZJ
 * @author dan
 *
 */
@Component("persistentVisitor")
public class PersistentVisitor {
	
	private static final Logger logger = LoggerFactory.getLogger(PersistentVisitor.class);
	
	@Autowired
	private DataService<District> dataService;
	
	@Autowired
	private GenericDaoSupport genericDaoSupport;
	
	@Autowired
    private DataService<Role> roleService;
	
	@Autowired
	private CountryService countryService;
	
	@Autowired
	@Qualifier("dataService")
	private DataService<City> cityService;
	
	@Autowired
	@Qualifier("dataService")
	private DataService<CountryCoupon> countryCouponService;
	
	@Autowired
	@Qualifier("dataService")
	private DataService<SetCategory> setCategorService;
	@Autowired
	private SupplierService supplierService;
	
	@Autowired
	private SetCategoryService setCategoryService;	
	
	@Autowired
	private CurrencyService currencyService;
	
	@Autowired
	private PaymentRequestService paymentRequestService;
	
	@Autowired
	private SetService setService;
	
	@Autowired
	private PrincipalService principalService;

	
	/*
	 * @Autowired private OrderSpecialEquipmentService orderSpecialEquipmentService;
	 */
	
	/*
	 * @Autowired
	 * 
	 * @Qualifier("dataService") private DataService<SpecialEquipment>
	 * specialEquipmentService;
	 */
	/*
	 * @Autowired private SpecialEquipmentService specialEquipmentService;
	 */
	/**
	 * 	
	 * @return
	 */
	public List<Role> getRoles(){
		
		logger.debug("#getRole() - get role from database");
		
		List<Role> role = roleService.listAll(Role.class);
		
		return role;
	}
	
	/**
     *  
     * @return
     */
    public List<District> getDistricts(){
        
        logger.debug("#getDistrict() - get district from database");
        
        List<District> district = dataService.listAll(District.class);
        
        return district;
    }
	
	/**
	 * 
	 * @return
	 */
	public List<Country> getCountries() {
	    List<Country> countries = countryService.listAll(Country.class);
	    return countries;
	}
	
	/**
	 * 
	 * @return
	 */
	public List<City> getCities() {
	    List<City> cities = cityService.listAll(City.class);
	    return cities;
	}
	
	/**
	 * 
	 * @param code
	 * @return
	 */
	public Country country(String code) {
	    return countryService.load(code);
	}
	
	/**
	 * 
	 * @param code
	 * @return
	 */
	public Supplier supplier(String code) {
		return supplierService.load(code);
	}
	
	/**
	 * 
	 * @param code
	 * @return
	 */
	public SetCategory setCategory(String code) {
		return setCategoryService.load(code);
	}
	
	public SetCategory setCategorys(String code) {
		Set set = setService.load(code);
		return setCategorService.load(SetCategory.class, set.getCategoty().getId());
	}
	/**
	 * 
	 * @param code
	 * @return
	 */
	public ExchangeRate exchangeRate(String code) {
		return currencyService.load(code);
	}
	
	/**
	 * 
	 * @param code
	 * @return
	 */
	public PaymentRequest paymentRequest(String code) {
		
		 PaymentRequest load = paymentRequestService.load(code);
		 return load;
	}

	/**
	 *
	 * @param orderCode
	 * @return
	 */
	public PaymentRequest loadPaymentRequestByOrderCode(String orderCode){

		return paymentRequestService.loadByOrderCode(orderCode);
	}
	
	/**
	 * @param id
	 * @return
	 */
	public Principal principal(Long id) {
		
		Principal principal = principalService.load(Principal.class, id);
		
		return principal;
	}
	 
	/**
	 * 
	 * @param id
	 * @return
	 */
	public CountryCoupon coupon(Long id) {
		
		CountryCoupon coupon = countryCouponService.load(CountryCoupon.class, id);
		
		return coupon;
	}
	
	
	  /**
     * 
     * @return
     */
    public List<CouponTemplate> getCouponTemplates() {
        return genericDaoSupport.loadAll(CouponTemplate.class);
    }
    
    
    /**
     * 
     * @param id
     * @return
     */
    public CouponTemplateStat couponTemplateStat(Long id) {
        return genericDaoSupport.load(CouponTemplateStat.class, id);
    }

	/**
	 *
	 * @param orderId
	 * @return
	 */
	public BigDecimal loadCnyPrice(Long orderId) {
		;
		Order order = genericDaoSupport.load(Order.class,orderId);
		Long couponId = order.getUserCouponId();
		Coupon coupon = genericDaoSupport.load(Coupon.class,order.getUserCouponId());
		Integer mount = coupon.getAmount();
		BigDecimal price = order.getCnyPayPrice(coupon.getAmount());
		return order.getCnyPayPrice(coupon.getAmount());
	}

	/**
	 *
	 * @param userCouponId
	 * @return
	 */
	public Coupon loadUserCoupon(Long userCouponId){

		Coupon coupon = genericDaoSupport.load(Coupon.class, userCouponId);

		return coupon;
	}

}
